1 00:00:01,020 --> 00:00:02,100 Welcome back. 2 00:00:02,100 --> 00:00:03,120 In this lecture. 3 00:00:03,120 --> 00:00:05,940 We're going to be implementing a day time cycle. 4 00:00:05,940 --> 00:00:08,940 Now I've already covered this system in a previous lecture. 5 00:00:08,940 --> 00:00:12,060 But we're going to go through it again once more for this project. 6 00:00:12,060 --> 00:00:16,320 So go ahead and create a new module script underneath our server script service. 7 00:00:16,320 --> 00:00:18,330 That's going to act as a new service. 8 00:00:18,330 --> 00:00:22,620 And we can call this module our Time Cycle service. 9 00:00:23,320 --> 00:00:27,940 Then I'm going to paste in that thing we've been using or that template we've been using, and I'll 10 00:00:27,940 --> 00:00:30,790 rename this to our time cycle service. 11 00:00:31,900 --> 00:00:35,650 Inside of this module, we're going to need the lighting service. 12 00:00:39,260 --> 00:00:41,810 As well as the run service. 13 00:00:42,990 --> 00:00:46,620 And we're also going to be using the tween service. 14 00:00:52,460 --> 00:00:58,010 Now, I also want to go ahead and start playing a sound in the workspace when the time turns to night. 15 00:00:58,010 --> 00:01:02,840 So inside of the workspace I have a folder called Global Sounds and then we have some ambience to play 16 00:01:02,840 --> 00:01:05,540 when it's night time, so I'll make a quick reference to that. 17 00:01:05,540 --> 00:01:08,540 In this script we'll call it night ambience. 18 00:01:09,260 --> 00:01:14,450 That's equal to workspace dot global sounds and we'll get night ambience. 19 00:01:15,300 --> 00:01:19,620 Now we can go ahead and define some constants for our module. 20 00:01:19,620 --> 00:01:23,130 One would be how long we would like the day to last in seconds. 21 00:01:23,130 --> 00:01:25,470 So for example we can set it to 300. 22 00:01:25,470 --> 00:01:30,510 And that means the entire, uh day time cycle would last 300 seconds. 23 00:01:30,690 --> 00:01:34,770 We can set what hour we would like to start the day at, so I'll call it start hour. 24 00:01:34,770 --> 00:01:37,140 And we could set it at something like 9 a.m.. 25 00:01:37,470 --> 00:01:41,160 And then we're going to define the max brightness. 26 00:01:41,160 --> 00:01:43,260 So this is going to be the brightness set. 27 00:01:43,260 --> 00:01:44,250 And the lighting service. 28 00:01:44,250 --> 00:01:47,880 When we're in the daytime we could set it to three. 29 00:01:47,880 --> 00:01:50,460 And then we can go ahead and define a minimum brightness. 30 00:01:50,460 --> 00:01:53,550 And this is the brightness we're going to set when it's nighttime. 31 00:01:53,880 --> 00:02:00,300 And then we can calculate a brightness delta which is something we're going to increment the brightness 32 00:02:00,300 --> 00:02:05,430 in the lighting service every single time a second goes by. 33 00:02:05,430 --> 00:02:08,850 So we can set this equal to the max brightness. 34 00:02:08,850 --> 00:02:11,550 And then just divide that by the day length. 35 00:02:11,910 --> 00:02:17,430 So that gives us our delta and then I'm going to create a variable I'm going to call it start. 36 00:02:17,430 --> 00:02:25,470 And this is going to represent the current time or the current tick based on what our start hour is 37 00:02:25,470 --> 00:02:26,100 going to be. 38 00:02:26,100 --> 00:02:27,900 So we're going to set equal to the tick. 39 00:02:27,900 --> 00:02:32,040 And we need to subtract it by our start hour. 40 00:02:32,040 --> 00:02:35,130 We get our start our going to divide it by 24. 41 00:02:35,130 --> 00:02:37,920 And then we're going to multiply that by the day length. 42 00:02:37,920 --> 00:02:43,800 So we're going to get start hour divided by 24 and then multiply it by our day length. 43 00:02:45,480 --> 00:02:52,350 So basically this is going to give us, um, a number here to subtract the current tick by to represent 44 00:02:52,350 --> 00:02:55,710 how many seconds we have progressed so far in the day. 45 00:02:55,710 --> 00:03:01,560 If we have set it at 9 a.m., then that means this value here is going to represent how many seconds 46 00:03:01,560 --> 00:03:04,830 has elapsed for it to be 9 a.m.. 47 00:03:04,830 --> 00:03:10,260 So nine divided by 24 is that fraction or the percentage of how long we progressed through the day. 48 00:03:10,260 --> 00:03:13,980 And then we just multiply that by the day length to get that number of seconds. 49 00:03:13,980 --> 00:03:16,980 And then we subtract it to the current tick. 50 00:03:16,980 --> 00:03:19,080 So that way we get our start time. 51 00:03:20,240 --> 00:03:23,480 And then we can go ahead and define a private function. 52 00:03:23,480 --> 00:03:28,100 And I'm going to call this on heartbeat because we're going to connect to the heartbeat event on the 53 00:03:28,100 --> 00:03:35,480 run service to constantly update the time that has been elapsed and update the hour of day and the clock 54 00:03:35,510 --> 00:03:36,110 time. 55 00:03:37,160 --> 00:03:44,030 So in our start function, that's where we'll go ahead and refer to the heartbeat event and connect 56 00:03:44,030 --> 00:03:45,920 our own heartbeat function to it. 57 00:03:46,280 --> 00:03:52,190 And then in the initialize function what we're going to do is we're going to listen for when the clock 58 00:03:52,190 --> 00:03:54,530 time property in the lighting service changes. 59 00:03:54,530 --> 00:03:57,170 So get property change signal clock time. 60 00:03:57,940 --> 00:03:59,710 We'll connect and get a function. 61 00:03:59,710 --> 00:04:04,090 And this is where we can check what time it is, if it's daytime or if it's nighttime. 62 00:04:04,090 --> 00:04:07,330 If it's daytime, then we can stop the night ambience from playing. 63 00:04:07,330 --> 00:04:12,610 But if it's nighttime, then we can start playing the night ambience and we can go ahead and define 64 00:04:12,610 --> 00:04:14,020 daytime and night time. 65 00:04:14,020 --> 00:04:16,780 So for example, we could create a couple more constants. 66 00:04:17,330 --> 00:04:26,150 One we could define as day time start and we can set it to like maybe, I don't know, 6 a.m. and then 67 00:04:26,150 --> 00:04:31,250 we could go ahead and define another one and this will be our night time start. 68 00:04:31,250 --> 00:04:34,940 And actually let me go ahead and add a couple extra underscores. 69 00:04:34,940 --> 00:04:42,800 And then for the night time we could set it to maybe 18 which would be about 6:00. 70 00:04:43,010 --> 00:04:46,160 And then we could go ahead and check if the clock time. 71 00:04:46,160 --> 00:04:51,620 So if lighting service clock time is greater than our night time start. 72 00:04:52,340 --> 00:04:58,760 Or if our lighting service clock time is less than our day start. 73 00:04:59,650 --> 00:05:03,640 Then that means it's night time and we should start playing the night ambiance. 74 00:05:04,150 --> 00:05:09,640 So what we can do is we can refer to our night ambiance and start playing it. 75 00:05:09,640 --> 00:05:11,620 But I also want to like, fade it in. 76 00:05:11,620 --> 00:05:15,160 So we're going to set the night ambiance volume equal to zero. 77 00:05:15,160 --> 00:05:18,700 And then we can fade it in using the tween service. 78 00:05:18,700 --> 00:05:23,530 So we'll do tween service create on night ambience. 79 00:05:23,620 --> 00:05:26,680 We'll do a tween info dot new. 80 00:05:28,210 --> 00:05:29,890 And the duration we want to fade. 81 00:05:29,890 --> 00:05:34,330 The night ambience in is going to depend on our day length. 82 00:05:34,360 --> 00:05:39,430 So what I'll do is I'll create a variable, I'll call it tween duration, and it's going to be equal 83 00:05:39,430 --> 00:05:40,720 to the day length. 84 00:05:41,920 --> 00:05:44,950 And then we can just like multiply it by 0.5. 85 00:05:44,950 --> 00:05:52,030 So we'll cut it in half and then we'll multiply this value by another fraction I don't know something 86 00:05:52,030 --> 00:05:54,340 like 0.05. 87 00:05:54,520 --> 00:06:02,620 So for example if the day length is 300 seconds, what we could do is we could print out like 300 seconds 88 00:06:02,620 --> 00:06:06,370 times 0.5 and then multiply that by 0.05. 89 00:06:06,370 --> 00:06:12,520 So that means the total tween would last 7.5 seconds if the day length was like. 90 00:06:13,510 --> 00:06:17,680 Uh, let's say we want to set it to 1000 seconds, and then we enter that in. 91 00:06:17,680 --> 00:06:21,220 Then the tween, the total tween would last about 25 seconds. 92 00:06:21,860 --> 00:06:24,110 So it really depends on the day length. 93 00:06:24,350 --> 00:06:26,750 And then we'll just pass that tween duration here. 94 00:06:27,850 --> 00:06:31,540 And we'll just adjust the volume of our night ambience to be equal. 95 00:06:31,540 --> 00:06:32,320 Two. 96 00:06:32,350 --> 00:06:36,190 We could do a value of like 0.5 seconds. 97 00:06:36,970 --> 00:06:42,310 Now we also want to make sure that we don't play the night ambience again if it's already playing. 98 00:06:42,340 --> 00:06:46,990 So if our night ambience is already playing, so is playing. 99 00:06:46,990 --> 00:06:48,430 Then we're just going to return. 100 00:06:50,230 --> 00:06:51,700 And then. 101 00:06:52,650 --> 00:06:59,820 If the night ambience is playing right and it's currently nighttime. 102 00:07:00,780 --> 00:07:06,270 So what we want to do is we want to make sure the ambience is plain, and then we want to make sure 103 00:07:06,270 --> 00:07:09,660 the night ambience volume is equal to 0.5. 104 00:07:09,690 --> 00:07:13,200 This means we haven't tween the volume to fade out yet. 105 00:07:13,200 --> 00:07:21,660 So what we can do again is just copy this, paste this here and this time we want to tween the volume 106 00:07:21,660 --> 00:07:22,680 to be zero. 107 00:07:23,480 --> 00:07:28,730 And then instead of playing the tween directly, we'll get that tween in a variable. 108 00:07:30,930 --> 00:07:32,730 And then we'll play that tween. 109 00:07:33,920 --> 00:07:37,610 And then once that tween has completed. 110 00:07:38,240 --> 00:07:41,900 Then we can go ahead and stop the night ambience. 111 00:07:42,380 --> 00:07:46,310 And then the other thing we want to do for the initialize function is we want to go ahead and update 112 00:07:46,310 --> 00:07:49,700 the clock time property in the lighting service to be equal to the start hour. 113 00:07:49,700 --> 00:07:54,170 So lighting service clock time is going to be equal to start hour. 114 00:07:54,170 --> 00:07:59,330 And then we also want to update the uh brightness property. 115 00:07:59,330 --> 00:08:01,130 And that's going to be equal to. 116 00:08:01,130 --> 00:08:09,920 And we're going to check if the start hour is greater than the day time start. 117 00:08:10,480 --> 00:08:16,930 And we want to make sure that the start hour is less than the night time start. 118 00:08:16,930 --> 00:08:19,150 And if it is, that means it's the daytime. 119 00:08:19,150 --> 00:08:27,040 So we can set the brightness equal to the max brightness, else we'll set it to the minimum brightness. 120 00:08:30,060 --> 00:08:33,630 Okay, so now we can go ahead and fill out our on heartbeat function. 121 00:08:35,360 --> 00:08:40,280 So inside of here, what we want to do is we want to check the current time that has elapsed, and that's 122 00:08:40,280 --> 00:08:44,570 going to be equal to the current tick minus our start time. 123 00:08:45,020 --> 00:08:50,120 So as tick continues to update, we're slowly moving forward in time. 124 00:08:50,120 --> 00:08:55,760 And that will allow us to calculate the hour of day because we're getting how many seconds has elapsed 125 00:08:55,760 --> 00:08:56,300 so far. 126 00:08:56,300 --> 00:08:59,390 And then we'll compare that value to our day length. 127 00:09:00,750 --> 00:09:02,910 So we can go ahead and create a variable. 128 00:09:02,910 --> 00:09:04,290 I'll call it hour of day. 129 00:09:04,290 --> 00:09:11,370 And that's going to be equal to we're going to get our current time elapsed and divide that by our day 130 00:09:11,370 --> 00:09:12,030 length. 131 00:09:12,580 --> 00:09:18,610 And then we can go ahead and multiply that value by how many hours are in a day. 132 00:09:18,640 --> 00:09:20,410 So that would be 24. 133 00:09:20,410 --> 00:09:23,230 And this would give us the current hour of day. 134 00:09:24,710 --> 00:09:29,420 That means we're able to set the lighting server stock clock time equal to this hour. 135 00:09:29,900 --> 00:09:39,830 Now of course, we also want to check, um, if the current hour of day is greater than our day time 136 00:09:39,830 --> 00:09:46,640 start and our hour of day is less than the night time start. 137 00:09:46,970 --> 00:09:54,050 If it is, then we want to signify that the brightness should be set to max brightness. 138 00:09:54,050 --> 00:09:59,510 Otherwise the brightness should be set to the minimum brightness. 139 00:09:59,510 --> 00:10:00,830 So we can create a variable. 140 00:10:00,830 --> 00:10:02,810 I'll call it target brightness. 141 00:10:03,870 --> 00:10:07,860 And then we can set target brightness here equal to max brightness. 142 00:10:07,860 --> 00:10:11,760 Otherwise here we'll set target brightness equal to the minimum brightness. 143 00:10:13,890 --> 00:10:20,220 And then we can go ahead and check if the lighting service dot brightness is less than our target brightness. 144 00:10:20,220 --> 00:10:26,790 If that is the case, then we need to go ahead and increment lighting service dot brightness. 145 00:10:26,910 --> 00:10:34,500 That's going to be equal to math dot min, because we need to make sure it doesn't go beyond our max 146 00:10:34,500 --> 00:10:35,280 brightness. 147 00:10:35,280 --> 00:10:37,380 So we'll pass target brightness here. 148 00:10:37,380 --> 00:10:39,390 That's going to be the minimum number. 149 00:10:39,390 --> 00:10:46,650 And then we're going to pass lighting service dot brightness plus our brightness delta. 150 00:10:47,760 --> 00:10:51,060 Otherwise, if it's nighttime and we need to decrease the brightness. 151 00:10:51,060 --> 00:10:57,930 So if the lighting service dot brightness is greater than our target brightness, then what we need 152 00:10:57,930 --> 00:11:01,170 to do is exactly the same thing that we did up here. 153 00:11:01,170 --> 00:11:06,090 But this time we need to set the brightness equal to math dot max. 154 00:11:07,200 --> 00:11:08,970 Which would be zero. 155 00:11:08,970 --> 00:11:13,050 And then we're going to decrement by the brightness delta. 156 00:11:13,750 --> 00:11:20,200 And then another important thing we need to check is if the time that has elapsed so far is greater 157 00:11:20,200 --> 00:11:22,510 than or equal to our day length. 158 00:11:22,510 --> 00:11:28,900 If it is, then we need to update our start value and our start value is going to be updated to the 159 00:11:28,900 --> 00:11:29,950 current tick. 160 00:11:30,850 --> 00:11:34,570 So now we have this time cycle service setup. 161 00:11:34,570 --> 00:11:36,970 And if we go and playtest our game. 162 00:11:37,880 --> 00:11:39,800 And we go ahead and look at our lighting service. 163 00:11:39,800 --> 00:11:43,910 As you can see, the clock time, uh, value is slowly incrementing. 164 00:11:43,910 --> 00:11:50,330 And if we go ahead and take a look at the sun, as you can see, our sun is slowly moving and the entire 165 00:11:50,330 --> 00:11:53,630 24 hour cycle is going to take 300 seconds. 166 00:11:53,660 --> 00:11:56,810 Now, of course, we can also go in here and adjust this to be less. 167 00:11:56,810 --> 00:12:00,740 So maybe we only want the day to last a total of 60s. 168 00:12:00,740 --> 00:12:02,540 So we'll go and play test the game. 169 00:12:04,380 --> 00:12:08,010 And we can see the lighting service clock time is incrementing much faster. 170 00:12:08,010 --> 00:12:10,590 And if we look at the sun, as you can see, there it goes. 171 00:12:12,500 --> 00:12:16,460 And then we'll make sure once it hits nighttime that it'll start playing the night ambience. 172 00:12:16,460 --> 00:12:17,480 So here we go. 173 00:12:18,090 --> 00:12:18,570 There we go. 174 00:12:18,570 --> 00:12:20,190 Our night ambience faded in. 175 00:12:20,730 --> 00:12:22,080 There's the moon. 176 00:12:26,780 --> 00:12:30,110 And then we'll go ahead and see if our night ambience fades out as well. 177 00:12:33,090 --> 00:12:38,850 As you can see, our brightness has now increased and now we're back at the daytime and the night ambiance 178 00:12:38,850 --> 00:12:39,780 has faded out. 179 00:12:39,780 --> 00:12:40,620 Very cool. 180 00:12:41,770 --> 00:12:46,720 Now there is one more thing we need to add, which is updating the collision group for all of the players 181 00:12:46,720 --> 00:12:47,590 in our game. 182 00:12:47,770 --> 00:12:49,630 See you in the next lecture.